import * as crypto from 'crypto';

export const randomAuthCode = () => {
  let code = '';
  while (code.length < 5) {
    code += Math.floor(Math.random() * 10);
  }
  if (code[0] === '0') return '1' + code.slice(1, 5);
  return code;
};

/**
 * Encrypt password
 * @param password 密码
 * @param salt 盐值
 */
export function encryptPassword(password: string, salt: string): string {
  if (!password || !salt) {
    return '';
  }
  const tempSalt = Buffer.from(salt, 'base64');
  return (
    // 10000 代表迭代次数 16代表长度
    crypto.pbkdf2Sync(password, tempSalt, 10000, 16, 'sha1').toString('base64')
  );
}
